<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定时器</title>
    <script>
        window.onload = function () {
            var demo = document.getElementById("demo");
            var num = 0;
            var timer = setInterval(function () {
                if (num == 10){
                    clearInterval(timer);
                    return;
                }
                num++;
                demo.innerHTML = num;
            },1000);
        }
    </script>
</head>
<body>
    <div id="demo"></div>
</body>
</html>